import {ChangeDetectorRef, Component} from '@angular/core';

import { AboutPage } from '../about/about';
import { HomePage } from '../home/home';
import {ActionSheetController, App, NavController, Platform, PopoverController, ToastController} from "ionic-angular";
import {ZuopinPage} from "../zuopin/zuopin";
import {KepuPage} from "../kepu/kepu";
import {JidihttpProvider} from "../../providers/jidihttp/jidihttp";
import {BozhulistPage} from "../bozhulist/bozhulist";
import {HuodongPage} from "../huodong/huodong";

@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {
  pageno:number=1;
  xx:boolean=false;
  isaction:boolean=false;
  /*actionArray=[{"id":"asd"},{"id":"sdf"}];*/
  logintype:string;
  tab1Root=HomePage;
  tab2Root =ZuopinPage;
  tab4Root ;
  /** 活动 **/
  huodongMovies: Array<any>=[];
  /** 活动 基地 **/
  tasklisthuodong:Array<any>=[];//list列表
  constructor( public cdchange:ChangeDetectorRef,public popoverCtrl:PopoverController,public actionsheetCtrl: ActionSheetController,private app:App,public platform: Platform,public toastCtrl: ToastController,public navCtrl: NavController,public jidiProvider:JidihttpProvider) {
    /** 判断是否进入登陆 **/
    //this.loginType();
    /** 活动请求 **/
    this.huodonghttpClinet();
  }

  actionfun(id:string,name:string){
    location.href='http://www.bkqs8.com/photo/weChatApi/addWork?activityId='+id+"&activityName="+name;
  }

  uploadEvent(){
    if(this.tasklisthuodong.length==1){
      let any = this.tasklisthuodong[0];
      this.actionfun(any['id'],any['title']);
    }else {
      this.isaction = true;
    }
    /*this.logintype =localStorage.getItem('login');
    if("no"==this.logintype){
      this.presentActionSheet();
      //location.href='http://193.112.92.40/photo/weChatApi/login'
    }else{
       this.presentActionSheet();
      //location.href='http://193.112.92.40/photo/weChatApi/addWork'
    }*/
  }
  /** 上传活动 取消按钮 **/
  isactionqx(){
    this.isaction=false;
  }

  loginType(){
    this.logintype =localStorage.getItem('login');
    if("no"==this.logintype){
      location.href='http://www.bkqs8.com/photo/weChatApi/login'
    }else{
      this.tab4Root = AboutPage;
    }
    /*this.jidiProvider.getLoginType().subscribe(data => {
      if(data['success']){
        localStorage.setItem('login',"yes");
        this.tab4Root = AboutPage;
      }else{
        localStorage.setItem('login',"no");
        //this.app.getRootNav().push(LoginPage);
        location.href='http://ycsshj.oicp.net/photo/weChatApi/login'
      }

    },error => {

    });*/

  }

  /** 提示信息 **/
  showToast(position: string) {
    let toast = this.toastCtrl.create({
      message: "您好，"+position,
      duration: 2000,
      position: "bottom"
    });

    toast.present(toast);
  }


  openMenu() {
    this.xx=!this.xx;
    /*let actionSheet = this.actionsheetCtrl.create({
      title: '活动选择',
      buttons: [
        {
          text: '科普擂主',

          handler: () => {
            console.log('Delete clicked');
            this.app.getRootNav().push(BozhulistPage);
          }
        },
        {
          text: '科普活动',
          handler: () => {
            console.log('Share clicked');
            this.app.getRootNav().push(HuodongPage);
          }
        },
        {
          text: '科普基地',
          handler: () => {
            console.log('Play clicked');
            this.app.getRootNav().push(KepuPage);
          }
        },
      ]
    });
    actionSheet.present();*/
  }

  oneEvent(){
    this.openMenu();
    this.app.getRootNav().push(BozhulistPage);
  }
  twoEvent(){
    this.openMenu();
    this.app.getRootNav().push(HuodongPage);
  }
  threeEvent(){
    this.openMenu();
    this.app.getRootNav().push(KepuPage);
  }
  /** 活动详情 **/
  huodonghttpClinet(){

    this.jidiProvider.getHuodongDoingHttp().subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.huodongMovies.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.tasklisthuodong=this.huodongMovies;
        this.cdchange.detectChanges();
      }
    },error => {

    });
  }



  /** 活动选择 **/
  presentActionSheet(){
    const actionSheet = this.actionsheetCtrl.create({
      title: 'Modify your album',
      buttons: [
        {
          text: 'Destructive',
          role: 'destructive',
          handler: () => {
            console.log('Destructive clicked');
          }
        },{
          text: 'Archive',
          handler: () => {
            console.log('Archive clicked');
          }
        },{
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });
    actionSheet.present();
  }

}
